﻿using System;
using System.Activities;
using System.Collections.Generic;
using System.ComponentModel;

namespace Vision.Core.ActivityComponents.Collections
{
    /// <summary>
    /// 
    /// </summary>
    /// <typeparam name="TKey"></typeparam>
    /// <typeparam name="TValue"></typeparam>
    [Designer(typeof(Vision.Core.ActivityComponents.Collections.AddToDictionaryDesigner))]
    public class AddToDictionary<TKey, TValue> : CodeActivity
    {
        [RequiredArgument]
        public InArgument<IDictionary<TKey, TValue>> Dictionary { get; set; }
        
        [RequiredArgument]
        public InArgument<TKey> Key { get; set; }
        
        [RequiredArgument]
        public InArgument<TValue> Item { get; set; }

		public InArgument<bool> ReplaceIfKeyExists { get; set; }

		public AddToDictionary()
		{
			this.ReplaceIfKeyExists = false;
		}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        protected override void Execute(CodeActivityContext context)
        {
            IDictionary<TKey, TValue> dictionary = Dictionary.Get(context);
            TKey key = Key.Get(context);
            TValue item = Item.Get(context);
            if (dictionary != null){
				if (ReplaceIfKeyExists.Get(context) && dictionary.ContainsKey(key))
					dictionary[key] = item;
				else
					dictionary.Add(key, item);
            }
        }
    }
}
